package com.ruoyi.common.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

@Data
public class BalanceRecordEntity {

    public enum Type {
        NormalChat(1),
        AdvancedChat(2),
        Token(3),
        Draw(4);

        private int code;

        Type(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }

    public enum Source {
        Chat(1),
        Draw(2),
        AdminManual(3),
        Init(4);

        private int code;

        Source(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }

    public static Type getType(int code) {
        for (Type type : Type.values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        return null;
    }


    public static Source getSource(int code) {
        for (Source source : Source.values()) {
            if (source.getCode() == code) {
                return source;
            }
        }
        return null;
    }

    private int id;

    private int userId;

    private int balanceId;

    private Type type;

    private int typeId;

    private BigDecimal delta;

    private int apiKeyId;

    private int modelId;

    private int platformId;

    private int inputTokens;

    private int outputTokens;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="Asia/Shanghai")
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="Asia/Shanghai")
    private Date updateTime;

    private Source source;

    private int sourceId;

    private int state;

    private String reason;

    public static final int STATE_HANDLING = 0; // 扣减中，有可能废除
    public static final int STATE_NORMAL = 1;//正常，说明已经扣减成功，该记录生效
    public static final int STATE_DISCARDED = 2; // 废除，因为抛出了异常，所以此次扣减失效

    public static AiModelEntity.Level getLevelByType(Type type) {
        switch (type) {
            case NormalChat: return AiModelEntity.Level.NormalChat;
            case AdvancedChat: return AiModelEntity.Level.AdvanceChat;
            case Token: return AiModelEntity.Level.Token;
            case Draw: return AiModelEntity.Level.Draw;
            default: throw new IllegalArgumentException("unknown type: " + type);
        }
    }
}

